<?php
namespace app\index\controller;
use think\Db;
use my\Security;
use my\JoDES;
use think\Model;
use think\Controller;
class Ceshi extends Controller
{
	//
	public function index()
	{ 
		$page = input('get.page');
		$limit= input('get.limit');
		if($page && $limit){
			$start = ($page -1)*$limit;
			$list =  Db::table('api_test')->limit($start,$limit)->order('id desc')->select();
			$count =  Db::table('api_test')->limit($start,$limit)->order('id desc')->count();
			echo json_encode(array('code'=>0,
									'msg'=>'',
									'count'=>$count,
									'data'=>$list
								));
			exit;
		}

		return $this->fetch();
	}
	//按钮

	public function del(){
		$id = $_POST['id'];
		$res = Db::table('api_test')->where('id',$id)->delete();
		if($res){
			echo json_encode(['code'=>0]);
		}else{
			echo json_encode(['code'=>1]);
		}
	}
	public function add()
	{ 
		if(IS_POST){
			$data = $_POST;
			$data['question_time'] = date('Y-m-d H:i:s',time());

			Db::table('api_test')->insert($data);
			return "<script type='application/javascript'>parent.window.location.reload();</script>";
		}
		return $this->fetch();
	}
	public function edit()
	{ 
		if(IS_POST){
			$data = $_POST;
			$data['answer_time'] = date('Y-m-d H:i:s',time());
			Db::table('api_test')->update($data);
			return "<script type='application/javascript'>parent.window.location.reload();</script>";
		}else{
			$id = input('get.id');
			$data = Db::table('api_test')->where('id',$id)->find();
			$this->assign('data',$data);
			return $this->fetch();
		}
		
	}

}
